<?php
//设备管理
namespace App\Http\Controllers\App;

use App\Http\Controllers\App\BaseController as Controller;
use App\Models\Printer;
use App\Tools\CouldPrint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Shop;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ManagementDevice extends Controller
{
    protected $printer;
    protected $thisLoginClerk;

    public function __construct(Printer $printer,Request $request)
    {
        $this->printer = $printer;
        $checkThisClerk = $this->getShopUserID($request->token,$request->device_id);
        if(!$checkThisClerk){
            return show(2001,'无效token');
        }else{//判断用户角色
            switch($checkThisClerk->role){        //权限处理
                case -1:    // 商家
                case 2:     // 经理
                    $this->thisLoginClerk = $checkThisClerk;
                    break;
                case 0:     //财务
                case 1:     // 服务员
                case 3:     // 主管
                    return show(2002,'没有权限');
                    break;
                default:
                    return show(2002,'没有权限');
                    break;
            }
        }
    }

    //获取所有的打印机设备
    public function getAllPrinters(Request $request)
    {
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'shop_id.*'=> '商家信息有误',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }
        $res = $this->printer::where('shop_id',$request->shop_id)->get();
        if ($res->isEmpty()){
            return show(201,'暂无打印机信息');
        }
        return show(200,'信息获取成功',$res);
    }

    // 添加打印机设备
    public function addPrinter(Request $request){
        $rules = [
            'machinecode'=>'required|min:10',
            'machinekey' => 'required|min:10',
            'shop_id'=>'required|integer|exists:shops,id',
            'department'=>'string'
        ];
        $messages = [
            'machinecode.*'=> '机器码有误',
            'machinekey.*'=> '密钥有误',
            'shop_id.*'=> '商家不存在',
            'department.*'=>'部门填写有误'
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }
            }
            $count = Printer::where('machinecode',$request->machinecode)->count();
            if ($count>0){
                return show(201,'已存在，添加打印机失败');
            }
            $shopmsg = Shop::find($request->shop_id);
            $cloudPrint = new CouldPrint();
            $add_res = $cloudPrint->addPrint($request->machinecode,$request->machinekey,$shopmsg->shop_name);
            if ($add_res['error'] != '0'){
                return show(201,'添加打印机失败');
            }
            $res = $this->printer::create(\request(['shop_id','machinekey','machinecode','department']));
            if (!$res){
                return show(201,'添加打印机失败');
            }
            return show(200,'添加打印机成功');
        }catch (\Exception $e){
            return show(201,'添加打印机失败');
        }

    }

    // 展示打印机信息
    public function showOnePrinter(Request $request){
        $rules = [
            'id'=>'required|integer|exists:printers,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=> '打印机不存在',
            'shop_id.*'=> '商家不存在',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }
        $res = $this->printer::find($request->id);
        if (!$res){
            return show(201,'信息获取失败');
        }
        return show(200,'信息获取成功',$res);
    }
    // 编辑打印机信息
    public function editPrinter(Request $request){

        $rules = [
            'department'=>'required',
            'is_useall' => 'required|in:1,0',
            'id'=>'required|integer|exists:printers,id',
            'cuisine'=>'',
            'priter_number'=>'required|integer',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=> '打印机不存在',
            'is_useall.*'=>'请选择是否全部打印',
            'department.*' => '请认真填写所属部门',
            'priter_number.*'=>'打印联数填写有误',
            'cuisine.*'=>'菜系分类有误',
            'shop_id.*'=>'商家信息有误'
        ];

        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }
            }
            // 修改打印机
            $up_data = $request->all(['department','is_useall','cuisine','priter_number']);
            DB::table('printers')->where('id',$request->id)->update($up_data);
            return show(200,'修改成功');
        }catch (\Exception $e){
            return show(201,'修改失败');
        }

    }

    public function deleteOnePrinter(Request $request){
        $rules = [
            'id'=>'required|integer|exists:printers,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=> '打印机不存在',
            'shop_id.*'=> '商家不存在',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }

        try{
            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }
            }
            $printer = Printer::find($request->id);
            $code = $printer->machinecode;
            $cloudPrint = new CouldPrint();
            $add_res = $cloudPrint->delectPrint($code);
            $add_res = json_decode($add_res,true);
            if ($add_res['error'] != '0'){
                return show(2003,'删除打印机失败');
            }
            DB::table('printers')->delete($request->id);
            return show(200,'删除成功');
        }catch (\Exception $e){
            return show(201,'删除失败');
        }

    }
}
